package com.eliasbalasis.dwr4gwt.example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Random;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

@RemoteProxy
public class RemoteFunctions {
	private static Publisher publisher = new Publisher();
	private static Random random = new Random();
	private static long DELAY = 2000;

	@RemoteMethod
	public boolean getBoolean() {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return random.nextBoolean();
	}

	@RemoteMethod
	public double getNumber() {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return random.nextDouble();
	}

	@RemoteMethod
	public String getString() {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return "String "+new Long(random.nextLong()).toString();
	}

	@RemoteMethod
	public Collection<Person> getArray() {
		Collection<Person> persons = new ArrayList<Person>();
		Person person1 = new Person();
		person1.setName("person1");
		person1.setDescription("This is person1");
		person1.setIntValue(101);
		person1.setDateValue(new Date());
		person1.setFloatValue(0.101f);
		person1.setDoubleValue(Double.POSITIVE_INFINITY);
		person1.setBooleanValue(true);
		//
		Person person2 = new Person();
		person2.setName("person2");
		person2.setDescription("This is person2");
		person2.setIntValue(102);
		person2.setDateValue(new Date());
		person2.setFloatValue(0.102f);
		person2.setDoubleValue(Double.NaN);
		person2.setBooleanValue(false);
		//
		Person person3 = new Person();
		person3.setName("person3");
		person3.setDescription("This is person3");
		person3.setIntValue(103);
		person3.setDateValue(new Date());
		person3.setFloatValue(0.103f);
		person3.setDoubleValue(1.7976e308d);
		person3.setBooleanValue(false);
		persons.add(person1);
		persons.add(person2);
		persons.add(person3);
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return persons;
	}

	@RemoteMethod
	public Person getObject() {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		Person person = new Person();
		person.setName("aPerson");
		person.setDescription("This is a test person");
		person.setIntValue(8837);
		person.setDateValue(new Date());
		person.setFloatValue(7.421f);
		person.setDoubleValue(1.7976e308d);
		person.setBooleanValue(true);
		return person;
	}

	@RemoteMethod
	public Person getObjectFromParameters(
			String name,
			String description,
			int intValue,
			float floatValue,
			double doubleValue,
			boolean booleanValue,
			Date dateValue
			) {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		Person person = new Person();
		person.setBooleanValue(booleanValue);
		person.setDateValue(dateValue);
		person.setDescription(description);
		person.setDoubleValue(doubleValue);
		person.setFloatValue(floatValue);
		person.setIntValue(intValue);
		person.setName(name);
		return person;
	}

	@RemoteMethod
	public String sayHello(String name) {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return "Hello "+name;
	}

}
